﻿/*
 * @author Lee 
 * Time: 20180403 
 * Abstract: SharePoint API的二次封装
 */

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace SharePointHelper.DLL.SSOM
{
	public class View
	{
        #region 更改view的 properties
        /// <summary>
        /// 更改view的 properties
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isPaginate"></param>
        /// <param name="numberItems"></param>
        /// <returns></returns>
        public static bool UpdateView(SPView view, bool isPaginate, int? numberItems, 
			List<Caml.Predicate> camlPredicates, List<Caml.OrderByField> camlOrderByFields)
		{
			bool isSuccessful = false;
			try
			{
				// 初始化
				if (view == null) { throw new Exception("SPView passed is null"); }
				int defaultNumberItems = 30;
				if (numberItems == null) { numberItems = defaultNumberItems; }
				if (numberItems != null && numberItems > 2147483646) { throw new Exception("Row Limit for a view must be less than 2147483647"); }

				// 是否分页?
				view.Paged = isPaginate;

				// item的数量
				view.RowLimit = Convert.ToUInt32(numberItems);

				// 排序 & 过滤
				string camlQuery = null;
				if (camlPredicates != null && camlPredicates.Count > 0)
				{
					camlQuery += Caml.FormatPredicates(camlPredicates);
				}
				if (camlOrderByFields != null && camlOrderByFields.Count > 0)
				{
					camlQuery += Caml.FormatOrderBy(camlOrderByFields); 
				}
				if (!string.IsNullOrEmpty(camlQuery)) { view.Query = camlQuery; }

				// save changes to view
				view.Update();
				isSuccessful = true;
			}
			catch (Exception ex)
			{
				Log.WriteError("fgyjkugjfgrtdrfcxvcxzdfsuygf", ex);
				isSuccessful = false;
			}
			return isSuccessful;
		}

		public static bool UpdateView(SPView view, bool isPaginate, int? numberItems)
		{
			return UpdateView(view, isPaginate, numberItems, null, null);
		}
        #endregion

        #region 更改某个web的所有list中的默认的 view 的properties
        /// <summary>
        /// 更改某个web的所有list中的默认的 view 的properties
        /// But only if the list has a default view
        /// </summary>
        /// <param name="web"></param>
        /// <param name="isPaginate"></param>
        /// <param name="numberItems"></param>
        /// <returns></returns>
        public static bool UpdateAllListDefaultViews(SPWeb web, bool isPaginate, int? numberItems,
			List<Caml.Predicate> camlPredicates, List<Caml.OrderByField> camlOrderByFields)
		{
			bool isSuccessful = true;
			try
			{
				// 初始化
				if (web == null) { throw new Exception("SPWeb passed is null"); }


				for (int i = 0; i < web.Lists.Count; i++)
				{
					try
					{
						if (web.Lists[i].DefaultView != null)
						{
							bool _isSuccessful = UpdateView(web.Lists[i].DefaultView, isPaginate, numberItems,
								camlPredicates, camlOrderByFields);
							if (!_isSuccessful && isSuccessful) { isSuccessful = _isSuccessful; }
						}
					}
					catch (Exception _ex)
					{
						Log.WriteError("ytuygugddfsdsfdsf", _ex);
					}
				}
			}
			catch (Exception ex)
			{
				Log.WriteError("dtyioiukhvvncfgdstr", ex);
				isSuccessful = false;

			}
			return isSuccessful;
		}

        /// <summary>
        /// 更改某个web的所有list中的默认的 view 的properties
        /// 但只有当list具有默认视图时
        /// </summary>
        /// <param name="web"></param>
        /// <param name="isPaginate"></param>
        /// <param name="numberItems"></param>
        /// <returns></returns>
        public static bool UpdateAllListDefaultViews(SPWeb web, bool isPaginate, int? numberItems)
		{
			return UpdateAllListDefaultViews(web, isPaginate, numberItems, null, null);
		}
        #endregion
    }
}
